/**
 * 
 */
package info.niwota.fwrite;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;

/**
 * @author qiangli
 * 
 */
public class FdocView extends DocumentView {

	private static final String TAG = "FdocView";
	
	private CharSequence hint;
	private CharSequence text;
	private float textSize;
	private int textColor;
	private float hintSize;
	private int hintColor;
	private int maxLines;

	private Paint paint;

	private Drawable background;

	public FdocView(Context context) {
		super(context);

		initView();
	}

	public FdocView(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		initView();
		
		if (Debug.DEBUG) {
			Log.d(TAG, "attrs: " + attrs);
		}
		
		TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.FdocView);

		// hint
		CharSequence h = a.getString(R.styleable.FdocView_hint);
		if (h != null) {
			setHint(h.toString());
		}

		int hintColor = a.getColor(R.styleable.FdocView_hintColor, 0xff000000);
		if (hintColor > 0) {
			setHintColor(hintColor);
		}
		int hintSize = a.getDimensionPixelOffset(R.styleable.FdocView_hintSize,
				0);
		if (hintSize > 0) {
			setHintSize(hintSize);
		}
		
		// text
		CharSequence t = a.getString(R.styleable.FdocView_text);
		if (t != null) {
			setText(t.toString());
		}
		int textColor = a.getColor(R.styleable.FdocView_textColor, 0xff000000);
		if (textColor > 0) {
			setTextColor(textColor);
		}
		int textSize = a.getDimensionPixelOffset(R.styleable.FdocView_textSize,
				0);
		if (textSize > 0) {
			setTextSize(textSize);
		}
		
		//bg
        background = a.getDrawable(R.styleable.FdocView_background);
        super.setBackgroundDrawable(background);
		//
		a.recycle();
	}

	private void initView() {
		super.setViewMode(true);

		paint = super.getPaint();
		
		if (paint != null) {
			paint.setAntiAlias(true);
			paint.setTextSize(16);
			paint.setColor(Color.WHITE);
		}
		
		setPadding(3, 3, 3, 3);

	}

	//
	public CharSequence getHint() {
		return this.hint;
	}

	public int getHintColor() {
		return hintColor;
	}

	public float getHintSize() {
		return hintSize;
	}

	public int getMaxLines() {
		return maxLines;
	}

	public CharSequence getText() {
		return this.text;
	}

	public int getTextColor() {
		return textColor;
	}

	public float getTextSize() {
		return this.textSize;
	}

	public void setHint(CharSequence hint) {
		this.hint = hint;
		if (hint != null && isEmpty()) {
			addText(hint);
		}
	}

	public void setHintColor(int color) {
		this.hintColor = color;
		super.setColor(color);
		
		invalidate();
	}

	public void setHintSize(float size) {
		this.hintSize = size;
		super.setTextSize(size);
		
		requestLayout();
		invalidate();
	}

	public void setMaxLines(int maxlines) {
		this.maxLines = maxlines;
		requestLayout();
		invalidate();
	}

	public void setText(CharSequence text) {
		this.text = text;
		addText(text);
	}

	public void setTextColor(int color) {
		this.textColor = color;
		super.setColor(color);
		
		invalidate();
	}

	public void setTextSize(float size) {
		this.textSize = size;
		super.setTextSize(size);
		requestLayout();
		invalidate();
	}

	private void addText(CharSequence text) {
		super.clear();
		if (text != null) {
			Line line = new Line();
			line.add(new TextElement(text.toString()));
			super.addLine(line);
		}
		//
		requestLayout();
		invalidate();
	}
}
